﻿Imports System.IO

Public Class frmHightAir
    Private m_Projects As RadiosondeData = New RadiosondeData
    Public UTMPoint As New UTM.UTMPoint

    Private ArryUrlStr(4) As String '五个网址
    Private ArryUrlUniversit1() As String '多个月份的数据网址1
    Private ArryUrlUniversit2() As String '多个月份的数据网址2
    Private ArryUrlUniversit3() As String '多个月份的数据网址3
    Private ArryUrlUniversit4() As String '多个月份的数据网址4
    Private ArryUrlUniversit5() As String '多个月份的数据网址5
    Private mUrlStrs As String = "" '行到半个网址

    Private Sub StartDateTime_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartDateTime.ValueChanged
        '开始日期
        m_Projects.StartDateTime = Me.StartDateTime.Text
        EFlexAirData.Rows.Count = 1
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'EFlexAirData.Clear()
        EFlexAirData.Rows.Count = 1
        If EFlexAirData.Rows.Count < 2 Then
            Label1.Visible = True
            Label1.Text = "正在刷新数据，请等待....."
            Me.Refresh()
        Else
            Label1.Visible = False
        End If
        Dim latdd As Double
        Dim logdd As Double
        If Me.cmbDDEW.SelectedIndex = 0 Then
            latdd = txtLatDD.Value
            m_Projects.ZoneGive = 31 + Math.Truncate(txtLatDD.Value / 6)
        ElseIf Me.cmbDDEW.SelectedIndex = 1 Then
            latdd = 0 - txtLatDD.Value
            m_Projects.ZoneGive = 30 - Math.Truncate(txtLatDD.Value / 6)
        End If
        If Me.cmbDDNS.SelectedIndex = 0 Then
            logdd = txtLogDD.Value
        ElseIf Me.cmbDDNS.SelectedIndex = 1 Then
            logdd = 0 - txtLogDD.Value
        End If

        'Dim Cov As New UTM.Cover
        'UTMPoint = Cov.GeoToUtm(latdd, logdd, 32600 + m_Projects.ZoneGive)
        m_Projects.LongitudeValue = latdd
        m_Projects.LatitudeValue = logdd
        'm_Projects.LongitudeValue = UTMPoint.x
        'm_Projects.LatitudeValue = UTMPoint.y
        '得到选项数据

        m_Projects.ReadStrOption()
        'NOAA/ESRL 数据 得到URL
        If ComboBox2.SelectedIndex = 0 Then
            mUrlStrs = m_Projects.ShowNOAAESRL()
        End If
        If ComboBox2.SelectedIndex = 1 Then
            m_Projects.ShowAtmosphericSoundings() '获取网址
        End If
        '在窗体中得到数据列表
        meShowNOAAESRL()
        If EFlexAirData.Rows.Count < 2 Then
            Label1.Visible = True
            Label1.Text = "正在刷新数据，请等待....."
            Me.Refresh()
        Else
            Label1.Visible = False
        End If

    End Sub

    ''' <summary>
    ''' NOAA/ESRL 数据
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub meShowNOAAESRL()
        Dim mEndUrl As String = "&osort=Station+Series+Sort&oformat=FSL+format+%28ASCII+text%29"
        Dim mFormatStr As String = ""             '格式化字符
        Dim mNumber As Integer = 0                '字符串中的第N个字符串
        Dim mStationm As String = "&STNM="

        EFlexAirData.Rows.Count = 6
        EFlexAirData.SetData(0, 0, "大地线距离(km)")
        EFlexAirData.SetData(0, 1, "气象站WMO#")
        EFlexAirData.SetData(0, 2, "气象站名称")
        EFlexAirData.SetData(0, 3, "所在国家")
        EFlexAirData.SetData(0, 4, "气象站经度")
        EFlexAirData.SetData(0, 5, "气象站纬度")
        EFlexAirData.SetData(0, 6, "点击此获取数据")
        For j As Integer = 0 To 4
            For i As Integer = 0 To m_Projects.ArrDisSort.Length - 1
                If m_Projects.ArrDisSort(j) = m_Projects.RadiosondeDataOptions(i).Distances Then
                    Dim mList As String = ""                  '获得探空清单
                    EFlexAirData.SetData(j + 1, 0, (Math.Round(m_Projects.ArrDisSort(j), 0)).ToString)
                    EFlexAirData.SetData(j + 1, 1, m_Projects.RadiosondeDataOptions(i).StationNumber)
                    EFlexAirData.SetData(j + 1, 2, m_Projects.RadiosondeDataOptions(i).ArearNameTure)
                    EFlexAirData.SetData(j + 1, 3, "CN")
                    EFlexAirData.SetData(j + 1, 4, m_Projects.RadiosondeDataOptions(i).Longitude.ToString)
                    EFlexAirData.SetData(j + 1, 5, m_Projects.RadiosondeDataOptions(i).Latitude.ToString)
                    EFlexAirData.SetData(j + 1, 6, "点击此获取数据")

                    mNumber = StringHandle.GetStringNumber(m_Projects.RadiosondeDataOptions(i).AreasName)
                    If mNumber > 1 Then
                        For k As Integer = 1 To mNumber
                            mFormatStr = mFormatStr + StringHandle.GetString(m_Projects.RadiosondeDataOptions(i).AreasName, k) + "+"
                        Next
                    End If
                    mNumber = StringHandle.GetStringNumber(m_Projects.RadiosondeDataOptions(i).AreasName)
                    mList = mList + m_Projects.RadiosondeDataOptions(i).RegionStr + "+" + m_Projects.RadiosondeDataOptions(i).Atmospheric + "+" + m_Projects.RadiosondeDataOptions(i).StationNumber + "+" + m_Projects.RadiosondeDataOptions(i).Longitude.ToString + "+" + m_Projects.RadiosondeDataOptions(i).Latitude.ToString + "+" + m_Projects.RadiosondeDataOptions(i).HightId + "+" + mFormatStr + m_Projects.RadiosondeDataOptions(i).NotNameId + "+" + m_Projects.RadiosondeDataOptions(i).Identifier
                    If ComboBox2.SelectedIndex = 0 Then
                        ArryUrlStr(j) = mUrlStrs + mList + mEndUrl
                    End If
                    If ComboBox2.SelectedIndex = 1 Then
                        Select Case j
                            Case 0
                                For m As Integer = 0 To m_Projects.ArryUrlAtmos.Length - 1
                                    ReDim Preserve ArryUrlUniversit1(m)
                                    ArryUrlUniversit1(m) = m_Projects.ArryUrlAtmos(m) + mStationm + m_Projects.RadiosondeDataOptions(i).StationNumber
                                Next
                            Case 1
                                For m As Integer = 0 To m_Projects.ArryUrlAtmos.Length - 1
                                    ReDim Preserve ArryUrlUniversit2(m)
                                    ArryUrlUniversit2(m) = m_Projects.ArryUrlAtmos(m) + mStationm + m_Projects.RadiosondeDataOptions(i).StationNumber
                                Next
                            Case 2
                                For m As Integer = 0 To m_Projects.ArryUrlAtmos.Length - 1
                                    ReDim Preserve ArryUrlUniversit3(m)
                                    ArryUrlUniversit3(m) = m_Projects.ArryUrlAtmos(m) + mStationm + m_Projects.RadiosondeDataOptions(i).StationNumber
                                Next
                            Case 3
                                For m As Integer = 0 To m_Projects.ArryUrlAtmos.Length - 1
                                    ReDim Preserve ArryUrlUniversit4(m)
                                    ArryUrlUniversit4(m) = m_Projects.ArryUrlAtmos(m) + mStationm + m_Projects.RadiosondeDataOptions(i).StationNumber
                                Next
                            Case 4
                                For m As Integer = 0 To m_Projects.ArryUrlAtmos.Length - 1
                                    ReDim Preserve ArryUrlUniversit5(m)
                                    ArryUrlUniversit5(m) = m_Projects.ArryUrlAtmos(m) + mStationm + m_Projects.RadiosondeDataOptions(i).StationNumber
                                Next
                        End Select
                    End If
                End If
            Next
        Next
        EFlexAirData.AutoResize = True
        EFlexAirData.AutoSizeCols()
        EFlexAirData.AllowResizing = C1.Win.C1FlexGrid.AllowResizingEnum.None
        EFlexAirData.AllowResizing = C1.Win.C1FlexGrid.AllowResizingEnum.Columns
    End Sub

    Private Sub frmHightAir_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.EFlexAirData.Styles.Alternate.BackColor = Color.FromArgb(216, 228, 248)


        '开始时间与结束时间
        Me.EndDateTime.Text = "2008-12-31"
        Me.StartDateTime.Text = "2008-1-1"
        m_Projects.EndDateTime = EndDateTime.Text
        m_Projects.StartDateTime = Me.StartDateTime.Text

        ComboBox2.SelectedIndex = 0
        cmbDMSEW.SelectedIndex = 0
        cmbDDEW.SelectedIndex = 0
        cmbDMSNS.SelectedIndex = 0
        cmbDDNS.SelectedIndex = 0
        '经纬度
        txtLatDD.Value = 116.333
        txtLogDD.Value = 39.933

        Dim latdd As Double
        Dim logdd As Double
        If Me.cmbDDEW.SelectedIndex = 0 Then
            latdd = txtLatDD.Value
            m_Projects.ZoneGive = 31 + Math.Truncate(txtLatDD.Value / 6)
        ElseIf Me.cmbDDEW.SelectedIndex = 1 Then
            latdd = 0 - txtLatDD.Value
            m_Projects.ZoneGive = 30 - Math.Truncate(txtLatDD.Value / 6)
        End If
        If Me.cmbDDNS.SelectedIndex = 0 Then
            logdd = txtLogDD.Value
        ElseIf Me.cmbDDNS.SelectedIndex = 1 Then
            logdd = 0 - txtLogDD.Value
        End If

        Dim Cov As New UTM.Cover
        UTMPoint = Cov.GeoToUtm(latdd, logdd, 32600 + m_Projects.ZoneGive)
        m_Projects.LongitudeValue = UTMPoint.x
        m_Projects.LatitudeValue = UTMPoint.y

        DDtoDMS()
    End Sub

    Private Sub EndDateTime_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EndDateTime.ValueChanged
        '结束日期
        m_Projects.EndDateTime = EndDateTime.Text
        EFlexAirData.Rows.Count = 1
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged
        If RadioButton1.Checked = True Then
            m_Projects.Checkjw = True
            Panel2.Visible = False
            Panel1.Visible = True
        Else
            m_Projects.Checkjw = False
            Panel2.Location = Panel1.Location
            Panel1.Visible = False
            Panel2.Visible = True
        End If
    End Sub

    ''' <summary>
    ''' 按度
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub DDtoDMS()
        Dim dd As Double = txtLatDD.Value
        txtLatD.Value = Math.Truncate(dd)
        Dim mm As Double = 60 * (dd - Math.Truncate(dd))
        txtLatM.Value = Math.Truncate(mm)
        Dim ss As Double = mm - Math.Truncate(mm)
        txtLatS.Value = 60 * ss

        dd = txtLogDD.Value
        txtLogD.Value = Math.Truncate(dd)
        mm = 60 * (dd - Math.Truncate(dd))
        txtLogM.Value = Math.Truncate(mm)
        ss = mm - Math.Truncate(mm)
        txtLogS.Value = 60 * ss
    End Sub

    ''' <summary>
    ''' 按度分秒
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub DMStoDD()
        Dim dd As Double = txtLatD.Value
        Dim mm As Double = txtLatM.Value / 60
        Dim ss As Double = txtLatS.Value / 60 / 60
        txtLatDD.Value = dd + mm + ss

        dd = txtLogD.Value
        mm = txtLogM.Value / 60
        ss = txtLogS.Value / 60 / 60
        txtLogDD.Value = dd + mm + ss
    End Sub

    Private Sub cmbDMSEW_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDMSEW.SelectedIndexChanged
        cmbDDEW.SelectedIndex = cmbDMSEW.SelectedIndex
    End Sub

    Private Sub cmbDDEW_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDDEW.SelectedIndexChanged
        cmbDMSEW.SelectedIndex = cmbDDEW.SelectedIndex
    End Sub

    Private Sub txtLatD_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtLatD.Validating, txtLatM.Validating, txtLatS.Validating, txtLogD.Validating, txtLogM.Validating, txtLogS.Validating
        DMStoDD()
        EFlexAirData.Rows.Count = 1
    End Sub

    Private Sub txtLatDD_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtLatDD.Validating, txtLogDD.Validating
        DDtoDMS()
        EFlexAirData.Rows.Count = 1
    End Sub

    Private Sub EFlexAirData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EFlexAirData.Click
        m_Projects.mOnLine = True
        FileClose(1)
        If EFlexAirData.RowSel < 6 Then
            If EFlexAirData.ColSel = 6 Then
                Label1.Visible = True
                Label1.Text = "数据下载中，请等待....."
                Me.Refresh()
                'RichTextBox1.Clear()
                Dim mStrRichbox As String = ""
                m_Projects.PrintResultStr = ""
                If ComboBox2.SelectedIndex = 0 Then
                    '向网络发送一个请求得到回应
                    m_Projects.GetStrReslut(ArryUrlStr(EFlexAirData.RowSel - 1))
                    mStrRichbox = m_Projects.PrintResultStr
                    Dim StrDataBase As String = "<H1>Sorry:  No data is currently available for this request.<p>"
                    If m_Projects.PrintResultStr.Contains(StrDataBase) Then
                        MsgBox("对不起！网络数据请求失败" + vbCrLf + "请查看日期或网络状态！", MsgBoxStyle.Information, "提示")
                    Else
                        FileOpen(1, My.Application.Info.DirectoryPath & "\" & "AirData.txt", OpenMode.Output)
                        PrintLine(1, mStrRichbox) 'RichTextBox1.Text)
                        FileClose(1)
                        m_Projects.TranslateData(My.Application.Info.DirectoryPath & "\" & "AirData.txt")
                        Label1.Visible = False
                        Showfrm() '用表格来显示探空数据
                    End If
                End If
                If ComboBox2.SelectedIndex = 1 Then
                    '向网络发送一个请求得到回应
                    Dim mEFlexIndex As Integer = EFlexAirData.RowSel - 1
                    Dim StrDateBase1 As String = "Can't get"
                    FileOpen(1, My.Application.Info.DirectoryPath & "\" & "AirData.txt", OpenMode.Output)
                    Select Case mEFlexIndex
                        Case 0
                            For i As Integer = 0 To ArryUrlUniversit1.Length - 1
                                m_Projects.GetStrReslut(ArryUrlUniversit1(i))
                                If m_Projects.PrintResultStr.Contains(StrDateBase1) Then
                                    'MsgBox("对不起！没有得到您所请求的网络数据" + vbCrLf + "请查看日期或网络状态！", MsgBoxStyle.Information, "提示")
                                Else
                                    PrintLine(1, m_Projects.PrintResultStr)
                                    mStrRichbox = mStrRichbox + m_Projects.PrintResultStr
                                End If
                            Next
                        Case 1
                            For i As Integer = 0 To ArryUrlUniversit2.Length - 1
                                m_Projects.GetStrReslut(ArryUrlUniversit2(i))
                                If m_Projects.PrintResultStr.Contains(StrDateBase1) Then
                                    'MsgBox("对不起！没有得到您所请求的网络数据" + vbCrLf + "请查看日期或网络状态！", MsgBoxStyle.Information, "提示")
                                Else
                                    PrintLine(1, m_Projects.PrintResultStr)
                                    mStrRichbox = mStrRichbox + m_Projects.PrintResultStr
                                End If
                            Next
                        Case 2
                            For i As Integer = 0 To ArryUrlUniversit3.Length - 1
                                m_Projects.GetStrReslut(ArryUrlUniversit3(i))
                                If m_Projects.PrintResultStr.Contains(StrDateBase1) Then
                                    'MsgBox("对不起！没有得到您所请求的网络数据" + vbCrLf + "请查看日期或网络状态！", MsgBoxStyle.Information, "提示")
                                Else
                                    PrintLine(1, m_Projects.PrintResultStr)
                                    mStrRichbox = mStrRichbox + m_Projects.PrintResultStr
                                End If
                            Next
                        Case 3
                            For i As Integer = 0 To ArryUrlUniversit4.Length - 1
                                m_Projects.GetStrReslut(ArryUrlUniversit4(i))
                                If m_Projects.PrintResultStr.Contains(StrDateBase1) Then
                                    'MsgBox("对不起！没有得到您所请求的网络数据" + vbCrLf + "请查看日期或网络状态！", MsgBoxStyle.Information, "提示")
                                Else
                                    PrintLine(1, m_Projects.PrintResultStr)
                                    mStrRichbox = mStrRichbox + m_Projects.PrintResultStr
                                End If
                            Next
                        Case 4
                            For i As Integer = 0 To ArryUrlUniversit5.Length - 1
                                m_Projects.GetStrReslut(ArryUrlUniversit5(i))
                                If m_Projects.PrintResultStr.Contains(StrDateBase1) Then
                                    'MsgBox("对不起！没有得到您所请求的网络数据" + vbCrLf + "请查看日期或网络状态！", MsgBoxStyle.Information, "提示")
                                Else
                                    PrintLine(1, m_Projects.PrintResultStr)
                                    mStrRichbox = mStrRichbox + m_Projects.PrintResultStr
                                End If
                            Next
                    End Select
                    FileClose(1)
                    If mStrRichbox.Length > 10 Then
                        m_Projects.TranslateAtmospheric()
                        Label1.Visible = False
                        Showfrm()
                    Else
                        MsgBox("对不起！网络数据请求失败" + vbCrLf + "请查看日期或网络状态！", MsgBoxStyle.Information, "提示")
                    End If
                End If
                'RichTextBox1.Text = mStrRichbox
                Label1.Visible = False
            End If
        End If
        'Me.UpMetData.Type = True
    End Sub

    '调用探空气象数据显示表
    Private frm As New frmReconnaissance '.frmReconnaissance
    ''' <summary>
    ''' 调用探空气象数据显示表
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Showfrm()
        frm.UpMetData.Type = True
        frm.UpMetData.reconnaissance = m_Projects.ArrReconnaissance
        frm.UpMetData.title = m_Projects.RadiosTitle
        frm.CheckStr = True
        frm.ShowDialog()
    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        Me.EFlexAirData.Rows.Count = 1
    End Sub
End Class
